package com.jwdf.uda.ui.fragment.user.personal;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentIndividualitySignatureBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserUpdateInfoAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/21.
 */
public class IndividualitySignatureFragment extends BaseFragment {

    private FragmentIndividualitySignatureBinding mBinding;
    private String mIntroduction;
    private String mSignature;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBinding = FragmentIndividualitySignatureBinding.inflate(inflater, container, false);
        initView();
        return mBinding.getRoot();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }

    @Override
    public void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    private void initView(){
        mSignature = getArguments().getString("signature", "");
        mBinding.etNick.setText(mSignature);
        mBinding.etNick.setSelection(mSignature.length());
    }

    public void onEvent(IndividualEvent event) {
        switch(event.getEvent()) {
            case IndividualEvent.USER_INDIVIDUALITY_SIGNATURE:
                mIntroduction=mBinding.etNick.getText().toString();
//                if(mIntroduction.equals("")){
//                    Toast.makeText(getContext(),"个性签名为空",Toast.LENGTH_LONG).show();
//                }else {
                    updateUserInfo(mIntroduction);
//                }
                break;
        }
    }

    private void updateUserInfo(String introduction) {
        if(TextUtils.isEmpty(introduction)){
            introduction="空";
        }
        showProgressDialog();
        onNewRequestCall(UserUpdateInfoAction.newInstance(getContext(), introduction).request(new AHttpService.IResCallback<PbBean.PbResUserUpdateInfo>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserUpdateInfo> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    PbBean.PbResUserUpdateInfo info = response.body();
                    IndividualEvent event = new IndividualEvent(IndividualEvent.USER_INDIVIDUALITY_SIGNATURE);
                    event.userInfo = info.getUserInfo();
                    EventBusUtil.post(event);
                    getActivity().finish();
                }
            }
        }));
    }
}
